﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace T33
{
    class DBHelper
    {
        //连接字符串
        public static string ConnString = "server=.;database=GMP;Integrated Security=true";
        //连接对象
        public static SqlConnection conn = null;
        public static SqlDataReader Reader(string sql)
        {
            //创建连接对象
            conn = new SqlConnection(ConnString);
            //打开连接
            conn.Open();
            //创建命令对象
            SqlCommand cmd = new SqlCommand(sql, conn);
            //执行并返回结果
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;
        }
        public static bool NonQuery(string sql)
        {
            int result = 0;
            try
            {
                //创建连接对象
                conn = new SqlConnection(ConnString);
                //打开
                conn.Open();
                //创建命令对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //执行并返回结果
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine("捕获的异常信息：" + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return result > 0;
        }
        public static object Scalar(string sql)
        {
            conn = new SqlConnection(ConnString);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            object result = cmd.ExecuteScalar();
            return result;
        }
    }
}
